package com.itfenghuang.service.imp;

import com.itfenghuang.bean.User;
import com.itfenghuang.mapper.UserMapper;
import com.itfenghuang.service.UserService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 业务层的实现类
 */
@SuppressWarnings("all")
public class UserServiceImp implements UserService {

    @Override
    public String selectUserByUsername(User u) {
        InputStream is=null;
        SqlSession sqlSession=null;
        User user=null;
        try {
            //1,加载核心配置文件
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            //2.创建SqlSession工厂对象
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            //3.获取SqlSession对象
            sqlSession = factory.openSession();
            //4.获取对应接口的实现类
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            //5.获取方法
            user = mapper.selectUserByUsername(u.getUsername());
            //6.处理结果  进行数据验证
            //账号和密码是否正确  查询的结果为null,这个时候账号不对
            //如果u的密码和查询出来的user密码不一致,密码错误
            //账号或密码错误
            if (user==null || !user.getPassword().equals(u.getPassword())){
            //账号或密码不正确,将这句话返回
                return "账号或密码不正确";
            }else {
                return "登陆成功";
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
            if (is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "";
    }

    //添加
    @Override
    public String addUser(User u) {
        InputStream is=null;
        SqlSession sqlSession=null;
        User user=null;
        try {
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            sqlSession = factory.openSession(true);
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            user = mapper.selectUserByUsername(u.getUsername());
            if (user!=null){
                //查询出来了,说明该账号已存在
                return "用户名已存在";
            }else {
                //说明该账号可以被注册
                int i = mapper.addUser(u);
                return "添加成功";
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
            if (is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "";
    }

    //查询所有
    @Override
    public List<User> selectUser(User u){
        InputStream is=null;
        SqlSession sqlSession=null;
        List<User> list=null;
        try {
            is= Resources.getResourceAsStream("MyBatisConfig.xml");
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            sqlSession = factory.openSession();
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            list = mapper.selectUser(u);
            System.out.println(list);;
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
            if (is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }

    //修改密码
    @Override
    public String updatePassword(User u) {
        InputStream is=null;
        SqlSession sqlSession=null;
        User user=null;
        try {
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            sqlSession = factory.openSession(true);
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            int i = mapper.updatePassword(u);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
            if (is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "";
    }

    @Override
    public User selectUser1(String username) {
        InputStream is=null;
        SqlSession sqlSession=null;
        User user=null;
        try {
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            sqlSession = factory.openSession(true);
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            user = mapper.selectUserByUsername(username);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
            if (is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }


}
